package com.wang.common.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wang.common.pojo.InfoEnum;
import com.wang.common.util.VerifyCodeUtils;

/**
 * 公共的Controller
 * @author MyJJh
 *
 */
@Controller
@RequestMapping("/common/")
public class CommonController extends BaseController
{
	/**
	 * 验证码
	 * @param request
	 * @param response
	 * @throws IOException
	 * 如果方法的返回是void,那么就不再交给Spring管理
	 */
	@RequestMapping("/randImg")
	public void randImg(HttpServletRequest request, HttpServletResponse response) throws IOException 
	{
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		
		/**
		 * 随机数
		 */
		String ranCode = this.regexUtil.proccedRandStr(4, InfoEnum.VAR_NUMCHAR);
		
		//存入会话session
		HttpSession session = request.getSession();
		/* session中的键是randSess */
		session.setAttribute("randSess", ranCode);
		//生成图片
		int w = 250, h = 100;
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), ranCode);
	}
}
